package cn.iocoder.yudao.module.mqtt.controller.admin.mqtt;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mqtt.service.MultiMqttMessageSender;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * MQTT消息管理 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - MQTT消息管理")
@RestController
@RequestMapping("/mqtt/message")
@Validated
public class MqttMessageController {

    @Autowired
    private MultiMqttMessageSender multiMqttMessageSender;

    @PostMapping("/send")
    @Operation(summary = "发送消息到指定MQTT实例")
    @PreAuthorize("@ss.hasPermission('mqtt:message:send')")
    public CommonResult<Boolean> sendMessage(
            @Parameter(description = "MQTT配置ID", required = true) @RequestParam("configId") Long configId,
            @Parameter(description = "主题", required = true) @RequestParam("topic") String topic,
            @Parameter(description = "消息内容", required = true) @RequestParam("message") String message) {
        multiMqttMessageSender.sendMsg(configId, topic, message);
        return success(true);
    }

    @PostMapping("/send-with-qos")
    @Operation(summary = "发送消息到指定MQTT实例（指定QoS）")
    @PreAuthorize("@ss.hasPermission('mqtt:message:send')")
    public CommonResult<Boolean> sendMessageWithQos(
            @Parameter(description = "MQTT配置ID", required = true) @RequestParam("configId") Long configId,
            @Parameter(description = "主题", required = true) @RequestParam("topic") String topic,
            @Parameter(description = "QoS", required = true) @RequestParam("qos") Integer qos,
            @Parameter(description = "消息内容", required = true) @RequestParam("message") String message) {
        multiMqttMessageSender.sendMsg(configId, topic, qos, message);
        return success(true);
    }

    @PostMapping("/broadcast")
    @Operation(summary = "广播消息到所有MQTT实例")
    @PreAuthorize("@ss.hasPermission('mqtt:message:broadcast')")
    public CommonResult<Boolean> broadcastMessage(
            @Parameter(description = "主题", required = true) @RequestParam("topic") String topic,
            @Parameter(description = "消息内容", required = true) @RequestParam("message") String message) {
        multiMqttMessageSender.broadcastMsg(topic, message);
        return success(true);
    }

    @GetMapping("/instances")
    @Operation(summary = "获取所有运行的MQTT实例ID")
    @PreAuthorize("@ss.hasPermission('mqtt:message:query')")
    public CommonResult<Set<Long>> getRunningInstances() {
        Set<Long> instanceIds = multiMqttMessageSender.getRunningInstanceIds();
        return success(instanceIds);
    }

    @GetMapping("/status")
    @Operation(summary = "检查MQTT实例是否运行")
    @PreAuthorize("@ss.hasPermission('mqtt:message:query')")
    public CommonResult<Boolean> checkInstanceStatus(
            @Parameter(description = "MQTT配置ID", required = true) @RequestParam("configId") Long configId) {
        boolean isRunning = multiMqttMessageSender.isInstanceRunning(configId);
        return success(isRunning);
    }
} 